package com.pansoft.newasset.fragment;

import android.content.Context;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.recycleviewhelper.CommonAdapter;
import com.efounder.recycleviewhelper.base.ViewHolder;
import com.efounder.util.ToastUtil;
import com.efounder.utils.ResStringUtil;
import com.efounder.widget.RoundAngleImageView;
import com.pansoft.newasset.model.NewAssetDeviceModel;
import com.pansoft.openplanet.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 新版资产 -我的设备
 *
 * @author yqs
 * @date 2019/01/28
 */

public class NewAssetDeviceFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = "NewAssetTotalFragment";
    private View rootView;
    private TextView tvTips;
    private TextView tvTitleText;
    private RecyclerView recycleview;
    private TextView tvAddDevice;

    private DeviceAdater deviceAdater;
    private List<NewAssetDeviceModel> dataList;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = LayoutInflater.from(getActivity()).inflate(R.layout.new_asset_fragment_my_device, container, false);
        initView();
        initAdapter();
        requestData();
        return rootView;
    }

    private void initView() {
        tvAddDevice = (TextView) rootView.findViewById(R.id.tv_add_device);
        tvTips = (TextView) rootView.findViewById(R.id.tv_tips);
        tvTitleText = (TextView) rootView.findViewById(R.id.tv_title_text);
        recycleview = (RecyclerView) rootView.findViewById(R.id.recycleview);
        tvAddDevice.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_add_device) {
            ToastUtil.showToast(getActivity(), R.string.new_asset_add_device);
        }
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycleview.setLayoutManager(linearLayoutManager);
        dataList = new ArrayList<>();
        deviceAdater = new DeviceAdater(getActivity(), R.layout.new_asset_fragment_item_my_device, dataList);
        recycleview.setAdapter(deviceAdater);
    }

    private void requestData() {
        dataList.clear();
        NewAssetDeviceModel model1 = new NewAssetDeviceModel.Builder().deviceId("")
                .icon(R.drawable.new_asset_device_watch).name(ResStringUtil.getString(R.string.new_asset_smart_watch)).build();
        NewAssetDeviceModel model2 = new NewAssetDeviceModel.Builder().deviceId("")
                .icon(R.drawable.new_asset_device_loop).name(ResStringUtil.getString(R.string.new_asset_smart_shouhuan)).build();
        NewAssetDeviceModel model3 = new NewAssetDeviceModel.Builder().deviceId("")
                .icon(R.drawable.new_asset_device_shoe).name(ResStringUtil.getString(R.string.new_asset_smart_shoes)).build();
        NewAssetDeviceModel model4 = new NewAssetDeviceModel.Builder().deviceId("")
                .icon(R.drawable.new_asset_device_weigh).name(ResStringUtil.getString(R.string.new_asset_device_smart_weigh)).build();
        dataList.add(model1);
        dataList.add(model2);
        dataList.add(model3);
        dataList.add(model4);
        deviceAdater.notifyDataSetChanged();
    }

    public void refreshData() {
        requestData();
    }


    private class DeviceAdater extends CommonAdapter<NewAssetDeviceModel> {

        private List<NewAssetDeviceModel> list;
        private Context mContext;

        public DeviceAdater(Context context, int layoutId, List<NewAssetDeviceModel> datas) {
            super(context, layoutId, datas);
            this.mContext = context;
            this.list = datas;
        }

        @Override
        protected void convert(ViewHolder holder, NewAssetDeviceModel newAssetDeviceModel, int position) {
            RoundAngleImageView ivIcon;
            TextView tvDevicename;
            ivIcon = (RoundAngleImageView) holder.getView(R.id.iv_icon);
            tvDevicename = (TextView) holder.getView(R.id.tv_devicename);
            tvDevicename.setText(newAssetDeviceModel.getName());
            LXGlideImageLoader.getInstance().displayImage(mContext, ivIcon, newAssetDeviceModel.getIcon());
        }
    }

}
